# -*- coding: utf-8 -*-
import requests,urllib
from bs4 import BeautifulSoup
import datetime
from food.models import Food,Page


a = '农副产品价格'.decode('utf-8').encode('gbk')
for x in xrange(1,19):

    payload = {'PageNo': x, 'leixing': a}
    r = requests.get("http://www.tcprice.gov.cn/newscenter.asp", params=payload)
    r.encoding = 'gbk'
    print r.url
    soup = BeautifulSoup(r.text,'html5lib')
    tr_a = soup.find('td',{'class','erjiyemian_biaoti'}).parent
    trs = tr_a.find_next_sibling('tr').find('table').find_all('tr')
    for tr in trs:
        tds = tr.find_all('td')
        tda = tds[0].find('a').get('href')
        page_id = tda[13:]
        print page_id

        datestr = tds[1].contents[0].strip()
        page_date = datetime.datetime.strptime(datestr, '%Y-%m-%d').date()
        print page_date
        page_model = Page.objects.create_page(page_id=page_id,page_date=page_date)




